package com.lk.security.config;

import com.lk.security.authentication.mobile.SmsCodeSender;
import com.lk.security.authentication.mobile.SmsSend;
import com.lk.security.authentication.session.CustomInvalidSessionStrategy;
import com.lk.security.authentication.session.CustomSessionInformationExpiredStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.web.session.InvalidSessionStrategy;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

/**
 * @program: lk-security-parent
 * @description: 主要为容器中添加Bean实例
 * @author: Aspirin
 * @create: 2020-05-30 21:51
 */
@Configuration
public class SecurityConfigBean {

  @Autowired private SessionRegistry sessionRegistry;

  @Bean
  @ConditionalOnMissingBean(SessionInformationExpiredStrategy.class)
  public SessionInformationExpiredStrategy sessionInformationExpiredStrategy() {
    return new CustomSessionInformationExpiredStrategy();
  }

  /** 当session失效处理类 */
  @Bean
  @ConditionalOnMissingBean(InvalidSessionStrategy.class)
  public InvalidSessionStrategy invalidSessionStrategy() {
    return new CustomInvalidSessionStrategy(sessionRegistry);
  }

  /**
   * 注解@ConditionalOnMissingBean(SmsSend.class) 默认情况下，采用的是SmsCodeSender实例 容器上当中有其他的SmsSend类型的实例
   * 那当前的这个SmsCodeSender就失效了
   */
  @Bean
  @ConditionalOnMissingBean(SmsSend.class)
  public SmsSend smsSend() {
    // 这种写法相当与SmsCodeSender类加上@Component注解
    return new SmsCodeSender();
  }
}
